package com.ruoyi.business.controller;

import com.ruoyi.business.domain.BusPerson;
import com.ruoyi.business.domain.vo.NumListVo;
import com.ruoyi.business.mapper.BusPersonMapper;
import com.ruoyi.business.service.*;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DataMask;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 类功能描述： 额外功能控制器
 *
 * @author ling
 * @date 2024/08/01 10:36
 */
@RestController
@RequestMapping("/business/fun")
public class OtherfunctionController extends BaseController {

//    public

    @Resource
    private BusPersonMapper busPersonMapper;
    @Autowired
    private IBusStreetLedgerService busStreetLedgerService;
    @Autowired
    private IBusNeighborhoodLedgerService busNeighborhoodLedgerService;
    @Autowired
    private IBusDevicePressureService busDevicePressureService;
    @Autowired
    private IBusClinderGateService busClinderGateService;
    @Autowired
    private IBusPipeNetworkService busPipeNetworkService;
    @Autowired
    private IBusPipeService busPipeService;
    @Autowired
    private IBusMaintenanceService busMaintenanceService;
    @Autowired
    private IBusRepairService busRepairService;

    @Resource
    private ServerConfig serverConfig;
    @GetMapping("/numlist")
    public TableDataInfo list()
    {
        List<NumListVo> list = new ArrayList<>();

        Integer peoplenum = busPersonMapper.getCount();
        Integer streetnum = busStreetLedgerService.getCount();
        Integer neighbornum = busNeighborhoodLedgerService.getCount();

        Integer stationnum = busDevicePressureService.getStationCount();
        Integer cabinetnum = busDevicePressureService.getCabinetCount();
        Integer boxnum = busDevicePressureService.getBoxCount();
        Integer geotonenum = busDevicePressureService.getGeotoneCount();

        Integer cylindernum = busClinderGateService.getCylinderCount();
        Integer lock_wellnum = busClinderGateService.getLockWellCount();
        Integer pipe_networknum = busPipeNetworkService.getPipeNetworkCount();
        Integer introduction_tubenum = busPipeService.getIntroductionTubenum();
        Integer maintenancenum = busMaintenanceService.getMaintenanceCount();
        Integer repairnum = busRepairService.getRepairCount();
        Integer cyb_locknum = cylindernum + lock_wellnum ;
        Integer presuredevnum = stationnum + cabinetnum + boxnum + geotonenum ;
        Integer devicenum = presuredevnum + cyb_locknum + pipe_networknum;
        Integer tasknum = maintenancenum + repairnum;


        list.add(new NumListVo(peoplenum,streetnum,neighbornum,devicenum,tasknum,presuredevnum,stationnum,cabinetnum,boxnum,geotonenum,cyb_locknum,cylindernum,lock_wellnum,pipe_networknum,introduction_tubenum,maintenancenum,repairnum));
        logger.info("数据来了:");
        list.forEach(System.out::println);

        return getDataTable(list);
    }

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

}
